A correct way for JAVA age calculation? [closed]
Posted
by
Jhonnytunes
on Programmers
See other posts from Programmers
or by Jhonnytunes
Published on 2012-10-17T01:21:04Z
Indexed on
2012/10/17
5:24 UTC
Read the original article
Hit count: 232
java
I have already a Java calculation of age method. I have a Person Class where I have the method and I need to ask the current time each time the method is called. All I could do is make age a static field of person class, so all person classes use the same time now. Im worring about the Calendar.get() creating Calendar objects every time method is called. Am I doing it wrong? Can I make it better?
public short getAge(){
now = Calendar.getInstance();
return (short) ( (now.getTimeInMillis() - birthDate.getTimeInMillis())/ 31536000000L);
}
© Programmers or respective owner